<?php
namespace app\admin\controller;

use think\Db;
use app\admin\model\Member as user;
use think\Request;
use think\Validate;

class Icon extends Main
{
    private $db;

    public function  __construct() {
        parent::__construct();
        $this->db = Db::name('label_base_info');
    }

    public function index()
    {
        $this->assign('item',['item1'=>'网站管理','item2'=>'图标列表']);
        return  $this->fetch();
    }

    public function get_table_data()
    {
        //获取分页page和limit参数
        $page=input("get.page")?input("get.page"):1;
        $page=intval($page);
        $limit=input("get.limit")?input("get.limit"):10;
        $limit=intval($limit);
        $start=$limit*($page-1);

        $count = $this->db->count("id");
        $data_list = $this->db->limit($start,$limit)->order('id desc')->select();
        $list["msg"]="";
        $list["code"]=0; //状态码
        $list["count"]=$count;
        $list["data"]=$data_list;
        if(empty($data_list)){
            $list["msg"]="暂无数据";
        }
        return json($list);
    }

    public function add_icon()
   {
    if(Request::instance()->isPost()){//is post
        $post = $this->request->post();
        $validate = validate('Icon');
        $res = $validate->check($post);
        if($res !== true){//没有通过验证
           $this->error($validate->getError());
        }else{//通过验证

            $data = [
                    'zh_name'=>$post['zh_name'],
                    'eb_name'=>$post['eb_name'],
                    'price'=>$post['price'],
                    'label_code'=>'1',
                    'product_code' => '2',
                    'image_url_1'=>$post['image_url_1'],
                    'image_url_2' => $post['image_url_2'] ? $post['image_url_2'] : '',
                    'image_2d_url' => $post['image_2d_url'] ? $post['image_2d_url'] : '',
                    'image_3d_url' => $post['image_3d_url'] ? $post['image_3d_url'] : '',
                    'label_model' => $post['label_model'] ? $post['label_model'] : '',
                    'label_standard_id' => $post['label_standard_id'],
                    'label_applicable_area_id' => $post['label_applicable_area_id'],
                    'label_type_id' => $post['label_type_id'],
                    'label_content_id' => $post['label_content_id'],
                    'label_applicable_scene_id' => $post['label_applicable_scene_id'],
                    'label_material_id' => $post['label_material_id'],
                    'label_size_id' => $post['label_size_id'],
                    'label_description' => $post['label_description'] ? $post['label_description'] : '',
                    'label_technical_information' => $post['label_technical_information'] ? $post['label_technical_information'] : '',
                    'label_supplier' => $post['label_supplier'] ? $post['label_supplier'] : '',
                    'delivery_time' => $post['delivery_time'],
                ];
            $this->db->insert($data);
            setLog('增加标识','Icon/add_icon');
            $this->success('success');
        }
    }else{
        //标识标准表
        $label_standard_base_info = Db::name('label_standard_base_info')->select();
        //适用区域
        $applicable_area_base_info = Db::name('applicable_area_base_info')->select();
        //标识类别表
        $label_type_base_info = Db::name('label_type_base_info')->select();
        //标识样式表
        $label_content_base_info = Db::name('label_content_base_info')->select();
        //应用场景表
        $applicable_scene_base_info = Db::name('label_applicable_scene_base_info')->select();
        //标识尺寸表
        $size_base_info = Db::name('size_base_info')->select();
        //标识材质表
        $material_base_info = Db::name('material_base_info')->select();
        $this->assign('label_standard_base_info',$label_standard_base_info);
        $this->assign('applicable_area_base_info',$applicable_area_base_info);
        $this->assign('label_type_base_info',$label_type_base_info);
        $this->assign('label_content_base_info',$label_content_base_info);
        $this->assign('applicable_scene_base_info',$applicable_scene_base_info);
        $this->assign('size_base_info',$size_base_info);
        $this->assign('material_base_info',$material_base_info);
        return  $this->fetch();
    }
   }


   public function edit_icon()
   {
       $id = $this->request->get('id');
       if($id){
            $res = $this->db->where('id',$id)->find();
            $tmp = explode(' ',$res['delivery_time']);
            $res['delivery_time'] = array_shift($tmp);
            //标识标准表
            $label_standard_base_info = Db::name('label_standard_base_info')->select();
            //适用区域
            $applicable_area_base_info = Db::name('applicable_area_base_info')->select();
            //标识类别表
            $label_type_base_info = Db::name('label_type_base_info')->select();
            //标识样式表
            $label_content_base_info = Db::name('label_content_base_info')->select();
            //应用场景表
            $applicable_scene_base_info = Db::name('label_applicable_scene_base_info')->select();
            //标识尺寸表
            $size_base_info = Db::name('size_base_info')->select();
            //标识材质表
            $material_base_info = Db::name('material_base_info')->select();
            $this->assign('label_standard_base_info',$label_standard_base_info);
            $this->assign('applicable_area_base_info',$applicable_area_base_info);
            $this->assign('label_type_base_info',$label_type_base_info);
            $this->assign('label_content_base_info',$label_content_base_info);
            $this->assign('applicable_scene_base_info',$applicable_scene_base_info);
            $this->assign('size_base_info',$size_base_info);
            $this->assign('material_base_info',$material_base_info);
            $this->assign('data',$res);
            return  $this->fetch();
       }else{
           $this->error('非法操作');
       }
   }

   public function update_icon()
   {
    if(Request::instance()->isPost()){//is post
            $post = $this->request->post();
            $res = $this->validate($post,'Icon.edit');
            if($res !== true){//没有通过验证
               $this->error($res);
            }else{//通过验证
                $data = [
                    'zh_name'=>$post['zh_name'],
                    'eb_name'=>$post['eb_name'],
                    'price'=>$post['price'],
                    'image_url_2' => $post['image_url_2'] ? $post['image_url_2'] : '',
                    'image_2d_url' => $post['image_2d_url'] ? $post['image_2d_url'] : '',
                    'image_3d_url' => $post['image_3d_url'] ? $post['image_3d_url'] : '',
                    'label_model' => $post['label_model'] ? $post['label_model'] : '',
                    'label_standard_id' => $post['label_standard_id'],
                    'label_applicable_area_id' => $post['label_applicable_area_id'],
                    'label_type_id' => $post['label_type_id'],
                    'label_content_id' => $post['label_content_id'],
                    'label_applicable_scene_id' => $post['label_applicable_scene_id'],
                    'label_material_id' => $post['label_material_id'],
                    'label_size_id' => $post['label_size_id'],
                    'label_description' => $post['label_description'] ? $post['label_description'] : '',
                    'label_technical_information' => $post['label_technical_information'] ? $post['label_technical_information'] : '',
                    'label_supplier' => $post['label_supplier'] ? $post['label_supplier'] : '',
                    'delivery_time' => $post['delivery_time'],
                ];
                if($post['image_url_1']){
                    $data['image_url_1'] = $post['image_url_1'];
                }
                $this->db->where(['id' => $post['id']])->update($data);
                setLog('修改标识','Icon/add_icon');
                $this->success('success');
            }
       }else{
           $this->error('非法操作');
       }
   }

   public function deleteRow()
   {
       $id = $this->request->get('id');
       if($id){
           $res = $this->db->where('id',$id)->delete();
           if($res){
               setLog('删除图标','Icon/deleteRow');
            $this->success('success');
           }else{
               $this->error('删除失败');
           }
       }else{
           $this->error('非法操作');
       }  
   }

   public function bachDel(){
       if(Request::instance()->isPost()){//is post
           $post = $this->request->post();
           $id = implode(',',$post['id']);
           $this->db->whereIn('id',$id)->delete();
           $param = ['name'=>'批量删除图标','way'=>'Icon/bachDel','username'=>session('username'),'create_time'=>date('Y-m-d H:i:s',time())];
           \think\Hook::listen('log',$param);
           $this->success('success');
       }else{
           $this->error('非法操作');
       }
   }
   
   public function import_file()
   {
	   return  $this->fetch();
   }
   
  public function upload_file()
  {
	  //获取表单上传文件 
		$file = request()->file('files'); 
		if (empty($file)) { 
		  echo json_encode(array("code"=>999,"msg"=>"请选择上传文件","data"=>array()));die;
		} 
		//移动到框架应用根目录/public/uploads/ 目录下 
		$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'Icon'); 
		if ($info) {  
		  $name =  $info->getSaveName();
		  $oldName = $info->getInfo();
		  $name =  str_replace('\\' ,'/', $name);
		  $res = array("code"=>0,"msg"=>"","data"=>array('src'=>$name,'name'=>$oldName['name']));
		  echo json_encode($res);
		} else { 
		  //上传失败获取错误信息 
		  $this->error($file->getError()); 
		}
  }

   public function upload()
   {
       $file = request()->file('img');
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'Icon');
        if($info){
        $name =  $info->getSaveName();
        $name =  str_replace('\\' ,'/', $name);
        $res = array("code"=>0,"msg"=>"","data"=>array('src'=>'Icon/' . $name));
        echo json_encode($res);
        }else{
        $res = array("code"=>1,"msg"=>"","data"=>[]);
        // 上传失败获取错误信息
        echo $file->getError();
        } 
   }
   
	
	public function implode_data()
	{
		$post = $this->request->post();
		$file = ROOT_PATH . 'public'. DS . 'uploads'. DS . 'Icon'. DS . $post['fileName'];
		$file =  str_replace('\\' ,'/', $file);
		$type = pathinfo($file); 
		$type = strtolower($type["extension"]);
		if ($type=='xlsx') { 
			$type='Excel2007'; 
		}elseif($type=='xls') { 
			$type = 'Excel5'; 
		} 
		ini_set('max_execution_time', '0');
		vendor('excel.PHPExcel.IOFactory');
		vendor('excel.PHPExcel');
		//vendor('PHPExcel_IOFactory');
		// 判断使用哪种格式
		$objReader = \PHPExcel_IOFactory::createReader($type);
		$objPHPExcel = $objReader->load($file); 
		$sheet = $objPHPExcel->getSheet(0); 
		// 取得总行数 
		$highestRow = $sheet->getHighestRow();     
		// 取得总列数      
		$highestColumn = $sheet->getHighestColumn(); 
		//总列数转换成数字
		$numHighestColum = \PHPExcel_Cell::columnIndexFromString("$highestColumn");
		//循环读取excel文件,读取一条,插入一条
		$data=array();
		//从第一行开始读取数据
		for($j=1;$j<=$highestRow;$j++){
			//从A列读取数据
			for($k=0;$k<=$numHighestColum;$k++){
				//数字列转换成字母
				$columnIndex = \PHPExcel_Cell::stringFromColumnIndex($k);
				// 读取单元格
				$data[$j][]=$objPHPExcel->getActiveSheet()->getCell("$columnIndex$j")->getValue();
			} 
		}  
		var_dump($data);die;
		return $data;
	}
	
   
}